package com.ten.shou.service.impl;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ten.common.core.entity.CrmBanner;
import com.ten.common.core.vo.Result;
import com.ten.shou.dao.BannerDao;
import com.ten.shou.service.BannerService;
import com.ten.shou.vo.BannerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：杨（1964958771@qq.com）
 * @date ：Created in 2022/12/10 20:02
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class BannerServiceImpl implements BannerService {
    @Resource
    private BannerDao bannerDao;
    @Override
    public Result<CrmBanner> queryAllBanner(Integer current, Integer pageSize, BannerVo bannerVo) {
        IPage<CrmBanner> page=new Page<>(current,pageSize);
        QueryWrapper<CrmBanner> wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(bannerVo.getTitle())){
            wrapper.like("title",bannerVo.getTitle());
        }
        if (StringUtils.hasText(bannerVo.getIsDisable())){
            wrapper.eq("is_disable",bannerVo.getIsDisable());
        }
        IPage<CrmBanner> page1 = bannerDao.selectPage(page, wrapper);
        return new Result(2000,"查询成功",page1);
    }

    @Override
    public Result<CrmBanner> updateBottomStatus(String id, Integer isDisable) {
        UpdateWrapper<CrmBanner> wrapper=new UpdateWrapper<>();
        if (StringUtils.hasText(id)){
            wrapper.eq("id",id);
        }
        wrapper.set("is_disable",isDisable);
        bannerDao.update(null,wrapper);

        /*CrmBanner crmBanner = new CrmBanner();
        crmBanner.setId(id);
        crmBanner.setIsDisable(isDisable);
        bannerDao.updateById(crmBanner);*/
        return new Result<>(2000,"修改成功",null);
    }

    @Override
    public Result<CrmBanner> deleteByIdBanner(BannerVo bannerVo) {
        bannerDao.deleteById(bannerVo.getId());
        return new Result<>(2000,"删除成功");
    }

    @Override
    public Result<CrmBanner> updateBanner(CrmBanner crmBanner) {
        bannerDao.updateById(crmBanner);
        return new Result<>(2000,"修改成功");
    }

    @Override
    public Result<CrmBanner> insertBanner(CrmBanner crmBanner) {
        crmBanner.setIsDisable(0);
        bannerDao.insert(crmBanner);
        return new Result<>(2000,"添加成功");
    }

    @Override
    public Result getQianBanner() {
        QueryWrapper<CrmBanner> wrapper=new QueryWrapper<>();
        wrapper.eq("is_disable","0");
        List<CrmBanner> crmBanners = bannerDao.selectList(wrapper);
        return new Result(2000,"查询成功",crmBanners);
    }
}
